package ${package}.utils;

import com.ishansong.common.date.Dates;
import com.ishansong.common.security.MD5;

import java.util.Date;

/**
 * <pre>
 * 功能描述：
 *
 * </pre>
 * Author: sid
 * DateTime: 2017-11-21 10:19
 */
public class RedisKey {
    /**
     * 抢单锁的key
     *
     * @param orderNumber
     * @return
     */
    public static String buildGrabLockKey(String orderNumber) {
        return "lock_grab_" + orderNumber;
    }

    /**
     * 订单操作锁的key . (订单关键节点操作须先取锁——订单取消、抢单、完成)
     *
     * @param orderId
     * @return
     */
    public static String buildOrderOperationLockKey(Long orderId) {
        return "lock_order_operation_" + orderId;
    }

    /**
     * 订单的推送客户端列表key
     *
     * @param orderNumber
     * @return
     */
    public static String buildOrderPushListKey(String orderNumber) {
        return "push_list_" + orderNumber;
    }

    /**
     * 订单子任务完成锁key
     *
     * @param taskNumber
     * @return
     */
    public static String buildFinishTaskKey(String taskNumber) {
        return "lock_finish_" + taskNumber;
    }

    /**
     * 创建订单锁的key
     * @param orderNumber
     * @return
     */
    public  static String buildCreateLockOrderKey(String orderNumber){
        return "lock_create_"+orderNumber;
    }
    /**
     * 创建订单的key
     * @param orderNumber
     * @return
     */
    public  static String buildCreateOrderKey(String orderNumber){
        return "order_create_"+orderNumber;
    }
    /**
     * 创建批次订单的key
     * @param batchOrderNumber
     * @return
     */
    public  static String buildCreateBatchOrderKey(String batchOrderNumber){
        return "batch_order_create_"+batchOrderNumber;
    }

    /**
     * 订单埋点的key－下单以后使用
     *
     * @param orderNumber
     * @return
     */
    public static String buildBuryPointKey(String orderNumber) {
        return "order_bury_point_" + orderNumber;
    }
    /**
     * 下单埋点的key-仅下单使用
     *
     * @param orderNumber
     * @return
     */
    public static String buildBuryPlaceOrderKey(String orderNumber) {
        return "place_order_point_" + orderNumber;
    }

    /**
     * 个推的TaskId
     *
     * @param orderNumber
     * @return
     */
    public static String getGexinTaskKey(String orderNumber) {
        return "push.gexin.task." + orderNumber;
    }

    /**
     * 推送的静默开关标记
     *
     * @param mobile
     * @return
     */
    public static String getPushSilenceKey(String mobile) {
        return "push.silence." + mobile;
    }

    /**
     * 控制发短信的频次
     * @param taskNumber
     * @return
     */
    public static String buildSendMessageKey(String taskNumber){
        return "msg.send." + taskNumber;
    }

    /**
     * 闪送员评价用户KEY
     * @param orderNumber
     * @return
     */
    public static String buildRankUserKey(String orderNumber) {
        return String.format("rank_user_v1_%s", orderNumber);
    }

    /**
     * 批量下单处理进度
     * @param batchId
     * @return
     */
    public static String buildBatchOrderProcessKey(long batchId) {
        return String.format("batch_order_porcess_v1_%d", batchId);
    }

    /**
     * 批量下单-每一批的订单数量
     * @param batchId
     * @return
     */
    public static String buildBatchOrderCountKey(long batchId) {
        return String.format("batch_order_count_v1_%d", batchId);
    }

    /**
     * 生成城市+地址对应的KEY
     * @param city
     * @param address
     * @return
     */
    public static String buildWgs84PointKey(String city, String address) {
        String md5 = MD5.generate(city + address, false);
        return String.format("wgs84point_v1_%s", md5);
    }

    /**
     * 生成上个月订单标准时效KEY
     * @param key
     * @return
     */
    public static String buildStandSpeedKey(String key){
        return "current_standard_key" + key;
    }

    /**
     * 生成一个城市的全部闪送员姓名状态Map的KEY
     * @param cityId
     * @return
     */
    public static String buildCityCarrierNameStatusKey(int cityId){
        return "city_carrier_map_v1_" + cityId;
    }

    /**
     * 完成订单的Key
     * @param orderNumber
     * @return
     */
    public static String buildOrderFinishKey(String orderNumber){
        return "order_finish_v1_" + orderNumber;
    }

    /**
     * 用户类型中，是商户的（或者使用过余额支付的）ID集合
     * @return
     */
    public static String buildAllMerchantIdSetKey() {
        return "all_merchant_id_set_v1";
    }

    /**
     * key for is appoint
     *
     * @param orderNumber 订单号码
     * @return appoint key
     */
    public static String buildIsAppointKey(String orderNumber) {
        return "isappoint." + orderNumber;
    }

    /**
     * key for userinfo
     *
     * @param id 用户ID
     * @return userinfo key
     */
    public static String buildUserinfoKey(long id) {
        return "userinfo." + id;
    }

    /**
     * key for courier
     *
     * @param loginName 闪送员手机号码
     * @return courier key
     */
    public static String buildCourierKey(String loginName) {
        return "courier." + loginName;
    }

    /**
     * key for order task_to list
     *
     * @param orderNumber 订单号码
     * @return key for order task_to list
     */
    public static String buildOrderTaskToKey(String orderNumber) {
        return "order.taskto." + orderNumber;
    }

    /**
     * key for order task_from list
     *
     * @param orderNumber 订单号码
     * @return  key for order task_from list
     */
    public static String buildOrderTaskFromKey(String orderNumber) {
        return "order.taskfrom." + orderNumber;
    }

    /**
     * key for order payment
     *
     * @param orderNumber 订单号码
     * @return order payment key
     */
    public static String buildOrderPaymentKey(String orderNumber) {
        return "v1.order.payment." + orderNumber;
    }

    /**
     * key for order score
     *
     * @param orderNumber 订单号码
     * @return key for order score
     */
    public static String buildOrderScoreKey(String orderNumber) {
        return "order.score." + orderNumber;
    }

    /**
     * key for task score
     *
     * @param orderNumber 订单号码
     * @return key for task score
     */
    public static String buildTaskScoreKey(String orderNumber) {
        return "task.score." + orderNumber;
    }

    /**
     * key for Pending Order
     *
     * @param cityId 城市ID
     * @return key for Pending Order
     */
    public static String buildPendingOrderKey(Long cityId) {
        return "city_pending_order_" + cityId;
    }

    /**
     * key for order dto
     *
     * @param orderNumber 订单号码
     * @return key for order dto
     */
    public static String buildOrderDtoKey(String orderNumber) {
        return "order.dto." + orderNumber;
    }

    /**
     * key for 派单后的应答标识 (每个闪送员的应答标识是独立的)
     *
     * @param orderNumber 订单号码
     * @param courierMobile 闪送员手机号
     * @return key for 派单后的应答标识 (每个闪送员的应答标识是独立的)
     */
    public static String buildSysAssignResponseFlagKey(String orderNumber, String courierMobile) {
        return String.format("sys_assign_response_v1_%s_%s", orderNumber, courierMobile);
    }

    /**
     * key for 派单,派给了哪个闪送员
     *
     * @param orderNumber 订单号码
     * @return key for 派单,派给了哪个闪送员
     */
    public static String buildSysassignOrderCourierKey(String orderNumber) {
        return String.format("sysassign_order_courier_v1_%s", orderNumber);
    }

    /**
     * 闪送员拒绝派单结果列表
     * 只记录当天的结果
     *
     * @param courierMobile 闪送员手机
     * @return
     */
    public static String buildSysAssignRejectQueueKey(String courierMobile) {
        String todayStr = Dates.format(new Date(), "yyyyMMdd");
        return String.format("%s_sysassign_reject_queue_v1_%s", todayStr,courierMobile);
    }

    /**
     * 闪送员派单处罚标识
     *
     * @param courierMobile 闪送员手机
     * @return 闪送员派单处罚标识
     */
    public static String buildSysassignPunishKey(String courierMobile) {
        return String.format("sysassign_punish_v1_%s", courierMobile);
    }

    /**
     * 派单后,锁定闪送员一小段时间
     * @param courierMobile 闪送员手机
     * @return 派单后,锁定闪送员一小段时间
     */
    public static String buildSysassignCourierLockKey(String courierMobile) {
        return String.format("sysassign_courier_lock_v1_%s", courierMobile);
    }

    /**
     * redis存储mobile与client映射关系的key
     *
     * @param courierMobile 闪送员手机号码
     * @return redis存储mobile与client映射关系的key
     */
    public static String getMobileClientKey(String courierMobile){
        return "v4.push.client.mobile."+courierMobile;
    }

    /**
     * redis存储client信息的key
     * @param clientId 消息ID
     * @return redis存储client信息的key
     */
    public static String getClientKey(long clientId) {
        return "client." + clientId;
    }

    /**
     * 新增闪送员推荐奖励1隔越最多获取5次
     * @param start 开始时间
     * @param end 结束时间
     * @param courierId 闪送员ID
     * @return redisKey
     */
    public static String getRecomCourierKey(String start, String end, long courierId){

        return String.format("recom_courier_reward_%s_%s_%d",start,end,courierId);
    }

    /**
     * 收件人评价 链接 随机戳
     * @param orderNumber 订单
     * @param stamp  随机戳
     * @return
     */
    public static String getReceiverRankStampKey(String orderNumber,String toMobile,String stamp){
        return String.format("%s_rank_%s_stamp_%s",toMobile,orderNumber,stamp);
    }

    /**
     * 收件人评价 链接 随机戳
     * @param orderNumber 订单
     * @return
     */
    public static String getReceiverRankStampKey(String orderNumber,String toMobile){
        return String.format("%s_rank_%s",toMobile,orderNumber);
    }

    /**
     * key - 是否为老闪送员
     *
     * @param courierId
     * @return
     */
    public static String getOldCourierKey(Long courierId) {
        return "courier.isold." + courierId;
    }

    /**
     * key 派单时同一个订单闪送员只能被派一次
     * 高性能过滤缓存
     *
     * @param orderNumber
     * @param courierPhone
     * @return
     */
    public static String buildCourierIsAssignedKey(String orderNumber, String courierPhone) {
        return String.format("order_%s_assigned_to_courier_%s.v1", orderNumber, courierPhone);
    }

    /**
     * 加价重推订单总金额 key
     * 若发生了变化，则丢弃消息
     * @param orderId
     * @return
     */
    public static String buildOrderTotalFee4Additon(Long orderId) {
        return String.format("order_%s_total_payment_amount_4addition_v1", orderId);
    }

    /**
     * key for 派单-闪送员是否派送过回程单
     *
     * @param datePattern   指定日期
     * @param courierMobile 闪送员手机号
     * @return
     */
    public static String buildSysAssignReturnOrderCourierKey(String datePattern, String courierMobile) {
        return String.format("sysassign_return_order_courier_v1_%s_%s", datePattern, courierMobile);
    }

    /**
     * key for 派单-回程单标记
     *
     * @param orderNumber   订单号码
     * @param courierMobile 闪送员手机号码
     * @return
     */
    public static String buildSysAssignReturnOrderKey(String orderNumber, String courierMobile) {
        return String.format("sysassign_return_order_v1_%s_%s", orderNumber, courierMobile);
    }

    public static String buildCourierIdKey(Long courierId){
        return String.format("courier_"+ courierId);
    }
    /**
     * 订单推荐的优惠券埋点
     * @param orderNumber
     * @return
     */
    public static String buildRecommendCoupon(String orderNumber){
        return String.format("order_recommend_coupon_%s",orderNumber) ;
    }

    /**
     * 初始化黑名单的加锁的key
     * @return
     */
    public static String buildInitUserBlackKey(){
        return "initUserBlackKey" ;
    }

    /**
     * 一键下单订单操作列表.
     * @param token
     * @return
     */
    public static String buildOrderingList(final String token, final String time) {
        return "user_ordering_list_" + time + "_" + token;
    }

}
